package com.sgb.kjwl.view.ui.activity.self.offer;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityTruckRigisterBinding;
import com.sgb.kjwl.view.constant.RouterKey;
import com.sgb.kjwl.view.ui.adapter.transport.CargoManageHomeTabAdapter;
import com.sgb.kjwl.view.ui.fragment.self.TruckApplyFragment;
import com.swgk.core.event.EventConstant;
import com.swgk.core.event.MessageEvent;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;
import com.swgk.core.util.UserOperating;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author : 熊丽
 * @date :2021/9/19 0019$ 16:30$
 * @Description ://货车报名管理类$
 */
public class TruckRegisterActivity extends AppActivity {

    ActivityTruckRigisterBinding binding;
    private boolean mIsGeneralGoods = true;
    //顶部tab
    private CargoManageHomeTabAdapter tabAdapter;

    //跳转操作
    public static void start(Context context, boolean isGeneralGoods) {
        Intent intent = new Intent(context, TruckRegisterActivity.class);
        intent.putExtra(RouterKey.BOOLEAN, isGeneralGoods);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void init() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_truck_rigister);
        binding.setLifecycleOwner(this);

        binding.include.titleView.setText("货车报名");
        binding.include.ivBack.setOnClickListener(v -> finish());
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction beginTransaction = manager.beginTransaction();
        beginTransaction.add(R.id.fragment, TruckApplyFragment.newInstance(false));
        beginTransaction.commit();

//        initTab();
//        initPager();
//        initEvent();
//        mIsGeneralGoods = getIntent().getBooleanExtra(RouterKey.BOOLEAN, true);
    }

    private void initTab() {
        List<String> tabs = new ArrayList<>();
        tabs.add("普通货源");
        tabs.add("竞价货源");
        tabAdapter = new CargoManageHomeTabAdapter(tabs);
        binding.rvTab.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.HORIZONTAL, false));
        binding.rvTab.setAdapter(tabAdapter);
    }

    private void initPager() {
        //设置加载当前页面左右两页内容
        binding.viewpager.setOffscreenPageLimit(1);
        //viewPager适配器
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(TruckApplyFragment.newInstance(false));
        fragments.add(TruckApplyFragment.newInstance(true));
        FragPagerAdapter pagerAdapter = new FragPagerAdapter(getSupportFragmentManager(), fragments);
        binding.viewpager.setAdapter(pagerAdapter);
    }

    private void initEvent() {
        binding.ivBack.setOnClickListener(v -> finish());
        binding.rightView.setVisibility(View.GONE);
        //
        tabAdapter.setOnItemClickListener((adapter, view, position) -> {
            tabAdapter.setSelectIndex(position);
            binding.viewpager.setCurrentItem(position);
        });
        binding.viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabAdapter.setSelectIndex(position);
                mIsGeneralGoods = (position == 0);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void fundAccountEvent(MessageEvent event) {
        //开通资金账户后刷新用户数据
        if (event.code == EventConstant.EVENT_REFRESH) {
            UserOperating.getInstance().refresh();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
